package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.TCargoDAO;
import com.smz.entity.Commodity;
import com.smz.entity.TCargo;
import com.smz.service.CommodityService;
import com.smz.service.TCargoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class TCargoServiceImpl extends ServiceImpl<TCargoDAO, TCargo> implements TCargoService {
    @Autowired
    private TCargoDAO tCargoDao;
    @Autowired
    private TCargoService tCargoService;
    @Autowired
    private CommodityService commodityService;

    @Override
    @Transactional
    public String moveGoods(TCargo tCargo) {
        Commodity commodity = commodityService.getOne(new QueryWrapper<Commodity>().lambda().eq(Commodity::getPName, tCargo.getProduct()));
        String catgoNo = tCargo.getBeforeCatgoNo();
        TCargo tCargoOriginal = tCargoService.getOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, catgoNo));
        Integer numOriginal = Integer.valueOf(tCargoOriginal.getNum());
        Integer numMove = Integer.valueOf(tCargo.getNum());
        if (numOriginal - numMove > 0) {
            tCargoOriginal.setNum(numOriginal - numMove);
            tCargoService.saveOrUpdate(tCargoOriginal, new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, catgoNo));
        }
        if (numOriginal - numMove == 0) {
            tCargoOriginal.setNum(numOriginal - numMove);
            tCargoOriginal.setProduct("");
            tCargoOriginal.setStatus("0");
            tCargoService.saveOrUpdate(tCargoOriginal, new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, catgoNo));
        }
        Random r = new Random();
        String s = String.valueOf(r.nextInt(1000));
        tCargo.setStatus("1");
        tCargo.setProduct(s + tCargoOriginal.getProduct());
        tCargoService.saveOrUpdate(tCargo, new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, tCargo.getCatgoNo()));
        commodity.setId(0);
        commodity.setPName(tCargo.getProduct());
        commodity.setPNum(tCargo.getNum());
        commodity.setPNumber(Integer.valueOf(s));
        commodityService.save(commodity);
        return "ok";
    }

    @Override
    public Page<TCargo> findPageTCargo(Integer current, Integer rows, Integer tShelvesNo, Integer damId, Integer wareId) {
        Page<TCargo> page = new Page<>(current, rows);
        Page<TCargo> data = tCargoDao.selectPage(page, new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo, tShelvesNo).eq(TCargo::getDamId, damId).eq(TCargo::getWareId, wareId));
        return data;
    }


    @Override
    public Page<TCargo> findPageTCargoSearch(Integer current, Integer rows, Integer tShelvesNo, Integer cargoNo, Integer cargoState) {

        Page<TCargo> page = new Page<>(current, rows);
        if (cargoNo != null) {
            Page<TCargo> data = tCargoDao.selectPage(page, new QueryWrapper<TCargo>().lambda().eq(TCargo::getCatgoNo, cargoNo));
            return data;
        } else {
            if (tShelvesNo != null && cargoState != null) {
                Page<TCargo> data = tCargoDao.selectPage(page, new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo, tShelvesNo).eq(TCargo::getStatus, cargoState));
                return data;
            } else if (tShelvesNo == null && cargoState != null) {
                Page<TCargo> data = tCargoDao.selectPage(page, new QueryWrapper<TCargo>().lambda().eq(TCargo::getStatus, cargoState));
                return data;
            } else if (tShelvesNo != null && cargoState == null) {
                Page<TCargo> data = tCargoDao.selectPage(page, new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo, tShelvesNo));
                return data;
            } else {

                return null;
            }
        }
    }

    @Override
    public IPage<TCargo> findPageTCargo(Integer size, Integer current, String dameId, String wareId, String shelvesNo) {
        return tCargoDao.selectPageTCargo(new Page(current,size),dameId,wareId,shelvesNo);
    }

    @Override
    public IPage<TCargo> selectTCargoByWareId(String wareId, String pNumber, String pName,int size,int current) {
        return tCargoDao.selectTCargoByWareId(new Page(current,size),wareId,pNumber,pName);
    }

    /**
     * @Authoe: smz
     * @Description: 根据仓库，库区，货架，货位查询是否存在商品
     * @Date: 10:29 2021-11-30
     */
    @Override
    public TCargo findTCargoByMenuIdAndMore(String menuId, String reservoirId, String shelvesId, String cargoId) {
        return tCargoDao.findTCargoByMenuIdAndMore(menuId,reservoirId,shelvesId,cargoId);
    }
}
